package com.example.touch.mediator;

import java.util.ArrayList;
import java.util.Collection;
import android.app.Activity;
import com.example.touch.communication.entities.Action;
import com.example.touch.communication.entities.ActionList;
import com.example.touch.communication.entities.ActionList.ActionExecutionType;
import com.example.touch.communication.entities.Notification;
import com.example.touch.communication.entities.PlaySound;
import com.example.touch.communication.entities.Vibrate;
import com.example.touch.communication.entities.OpenBrowser;
import com.example.touch.domain.ICompositeAction;
import com.example.touch.domain.IActivityAction;
import com.example.touch.domain.NotificationAction;
import com.example.touch.domain.OpenBrowserAction;
import com.example.touch.domain.ParallelActionInvoker;
import com.example.touch.domain.PlaySoundAction;
import com.example.touch.domain.SequentialActionInvoker;
import com.example.touch.domain.VibrationAction;

/**
*
* @author pla
*/
public class ActionMapper
{
	private Activity currentActivity;
	private ActivityActionFactory actionFactory;
	
	public ActionMapper(Activity currentActivity)
	{
		this.currentActivity = currentActivity;
		this.actionFactory = new ActivityActionFactory();
	}
	
	public ICompositeAction mapActionList(ActionList actionList)
	{
		ActionExecutionType executionType = actionList.getType();
		Collection<IActivityAction> resolvedActions = resolveActionCollection(actionList.getActions());
		ICompositeAction compositeAction = toCompositeAction(resolvedActions, executionType);
			
		return compositeAction;
	}
	
	protected ActivityActionFactory getActionFactory()
	{
		return this.actionFactory;
	}
	
	private ICompositeAction toCompositeAction(Collection<IActivityAction> resolvedActions, ActionExecutionType executionType)
	{
		ICompositeAction compositeAction = null;
		if (executionType == ActionExecutionType.parallel)
		{
			compositeAction = new ParallelActionInvoker(resolvedActions);
		}
		else if (executionType == ActionExecutionType.sequential)
		{
			compositeAction = new SequentialActionInvoker(currentActivity, resolvedActions, 3);
		}
		
		return compositeAction;
	}
	
	private Collection<IActivityAction> resolveActionCollection(Collection<Action> actions)
	{
		Collection<IActivityAction> activityActions = new ArrayList<IActivityAction>();
		
		for(Action action : actions)
		{
			IActivityAction resolvedAction = resolveAction(action);
			activityActions.add(resolvedAction);
		}
		
		return activityActions;
	}
	
	private IActivityAction resolveAction(Action action)
	{
		Class<?> actionClass = action.getClass();
		IActivityAction activityAction = null;
		
		if(actionClass == Vibrate.class)
		{
			activityAction = new VibrationAction(currentActivity, (Vibrate)action);
		}
		else if (actionClass == Notification.class)
		{
			activityAction = new NotificationAction(currentActivity, (Notification)action);
		}
		else if (actionClass == PlaySound.class)
		{
			activityAction = new PlaySoundAction(currentActivity, (PlaySound)action);
		}
		else if (actionClass == OpenBrowserAction.class)
		{
			activityAction = new OpenBrowserAction(currentActivity, (OpenBrowser)action);
		}
		
		return activityAction;
	}
	
	protected class ActivityActionFactory
	{
		protected IActivityAction createVibrateAction(String duration)
		{
			Vibrate v = new Vibrate();
			v.setDuration(duration);
			
			return new VibrationAction(currentActivity, v);
		}
		
		protected IActivityAction createNotificationAction(String message)
		{
			Notification n = new Notification();
			n.setMessage(message);
			
			return new NotificationAction(currentActivity, n);
		}
		
		protected IActivityAction createOpenBrowserAction(String url)
		{
			OpenBrowser o = new OpenBrowser();
			o.setUrl(url);
			
			return new OpenBrowserAction(currentActivity, o);
		}
		
		protected IActivityAction createSoundAction(String soundFileName)
		{
			PlaySound ps = new PlaySound();
			ps.setSoundID(soundFileName);
			
			return new PlaySoundAction(currentActivity, ps); // does not work - soundFileName must be known
		}

		protected IActivityAction createMediaAction(String mediaName)
		{
			return createSoundAction(mediaName); // Not currently supported
		}
	}
}
